﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class NhanVienDAO
    {
        public static bool DangNhap(string TenDN,string MatKhau)
        {
            List<NhanVienDTO> list = new List<NhanVienDTO>();
            String strSql = "select count(*) dem from DangNhap where TenDangNhap='" + TenDN + "' and MatKhau='" + MatKhau + "'";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                DataRow dr = dt.Rows[0];
                string dem = dr["dem"].ToString();
                if (dem.Equals("0"))
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }

        public static List<NhanVienDTO> SelectAll()
        {
            List<NhanVienDTO> list = new List<NhanVienDTO>();
            String strSql = "select * from NhanVien nv,ChucVu cv where ChucVu<>1 and nv.ChucVu=cv.MaCV";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    NhanVienDTO nv = new NhanVienDTO();
                    nv.MaNhanVien = int.Parse(dr["MaNV"].ToString());
                    nv.HoTen = dr["HoTen"].ToString();
                    nv.GioiTinh = dr["GioiTinh"].ToString();
                    nv.NgaySinh = dr["NgaySinh"].ToString();
                    nv.DiaChi = dr["DiaChi"].ToString();
                    nv.SoDienThoai = dr["SoDienThoai"].ToString();
                    nv.MaChucVu = int.Parse(dr["ChucVu"].ToString());
                    nv.TenChucVu = dr["TenChucVu"].ToString();
                    list.Add(nv);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static bool Insert(NhanVienDTO nv)
        {
            bool result = false;

            string strSQLEmployee = "Insert into NhanVien " +
                "values (@Ho,@Gioi,@Ngay,@Dia,@So,@Chuc)";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@Ho", nv.HoTen));
                sqlParams.Add(new SqlParameter("@Gioi", nv.GioiTinh));
                sqlParams.Add(new SqlParameter("@Ngay", nv.NgaySinh));
                sqlParams.Add(new SqlParameter("@Dia", nv.DiaChi));
                sqlParams.Add(new SqlParameter("@So", nv.SoDienThoai));
                sqlParams.Add(new SqlParameter("@Chuc", nv.MaChucVu));
                // Call execute none query

                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Delete(int MaNV)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM NhanVien WHERE MaNV=@ID";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ID", MaNV));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Update(NhanVienDTO nv)
        {
            bool result = false;

            string strSQLEmployee = "Update NhanVien set HoTen=@Ho, GioiTinh=@Gioi, NgaySinh=@Ngay, DiaChi=@Dia,SoDienThoai=@So,ChucVu=@Chuc " +
                "Where MaNV=@ID";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ID", nv.MaNhanVien));
                sqlParams.Add(new SqlParameter("@Ho", nv.HoTen));
                sqlParams.Add(new SqlParameter("@Gioi", nv.GioiTinh));
                sqlParams.Add(new SqlParameter("@Ngay", nv.NgaySinh));
                sqlParams.Add(new SqlParameter("@Dia", nv.DiaChi));
                sqlParams.Add(new SqlParameter("@So", nv.SoDienThoai));
                sqlParams.Add(new SqlParameter("@Chuc", nv.MaChucVu));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
